﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;

namespace SaveMeGUI
{
    class XMLParser
    {


        public XMLParser()
        {

        }


        public List<string[]> GetRequests()
        {
            List<string[]> listRequests = new List<string[]>();
            string url = "http://balder.ucn.dk/dm77_gr2/SaveMe.svc/GetTaxiRequestsByStatus";

            XmlDocument xmlDoc = MakeRequest(url);

            XmlNodeList requests = xmlDoc.GetElementsByTagName("TaxiRequest");

            foreach (XmlNode node in requests)
            {

                XmlNodeList requestInfoXML = node.ChildNodes;

                string[] singleRequest = new String[9];

                foreach (XmlNode infoNode in requestInfoXML)
                {
                    if (infoNode.Name.Equals("id"))
                    {
                        singleRequest[0] = infoNode.InnerText;
                    }

                    if (infoNode.Name.Equals("date"))
                    {
                        string input_date = infoNode.InnerText;
                        int result_month = System.Convert.ToInt32(input_date.Substring(3, 2));
                        string month = "";                        

                        switch (result_month)
                        {
                            case 1: month = "January";
                                break;
                            case 2: month = "February";
                                break;
                            case 3: month = "March";
                                break;
                            case 4: month = "April";
                                break;
                            case 5: month = "May";
                                break;
                            case 6: month = "June";
                                break;
                            case 7: month = "July";
                                break;
                            case 8: month = "August";
                                break;
                            case 9: month = "September";
                                break;
                            case 10: month = "October";
                                break;
                            case 11: month = "November";
                                break;
                            case 12: month = "December";
                                break;
                        }

                        string result = input_date.Substring(0, 2) + " " + month + "   " + input_date.Substring(11, 5);
                        
                        singleRequest[1] = result;
                    }

                    if (infoNode.Name.Equals("address"))
                    {
                        string input = infoNode.InnerText;
                        string pattern = "_";
                        string replacement = " ";

                        Regex rgx = new Regex(pattern);
                        string result = rgx.Replace(input, replacement);

                        singleRequest[2] = result;
                    }


                    if (infoNode.Name.Equals("homeAddress"))
                    {
                        string input = infoNode.InnerText;
                        string pattern = "_";
                        string replacement = " ";

                        Regex rgx = new Regex(pattern);
                        string result = rgx.Replace(input, replacement);

                        singleRequest[3] = result;
                    }

                    if (infoNode.Name.Equals("phoneNr"))
                    {
                        if (infoNode.InnerText.Equals("No_phone_number") || infoNode.InnerText == null)
                        {
                            singleRequest[4] = "";
                        }
                        else
                        {
                            singleRequest[4] = infoNode.InnerText;
                        }
                        
                    }

                    if (infoNode.Name.Equals("status"))
                    {
                        int status = int.Parse(infoNode.InnerText);

                        if (status == 1)
                        {
                            singleRequest[7] = "No taxi assigned";
                        }

                        else 
                        if (status == 2)
                        {
                            singleRequest[7] = "Taxi on the way";
                        }

                        else
                        if (status == 3)
                        {
                            singleRequest[7] = "Taxi arrived";
                        }
                        else if (status == 4)
                        {
                            singleRequest[7] = "Canceled";
                        }


                    }

                    if (infoNode.Name.Equals("plateNr"))
                    {
                        if (infoNode.InnerText.Equals("-") || infoNode.InnerText==null)
                            singleRequest[5] = "";
                        else
                            singleRequest[5] = infoNode.InnerText;
                    }

                    if (infoNode.Name.Equals("estimatedTime"))
                    {
                        if (infoNode.InnerText.Equals("-") || infoNode.InnerText == null)
                            singleRequest[6] = "";
                        else
                            singleRequest[6] = infoNode.InnerText;
                    }

                }



                listRequests.Add(singleRequest);


            }


            return listRequests;

        }

        private static XmlDocument MakeRequest(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(response.GetResponseStream());
                return (xmlDoc);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                Console.Read();
                return null;
            }
        }



    }
}
